package com.chat.service;

import com.chat.Local;
import com.chat.common.EmailMessage;
import com.chat.common.Message;
import com.chat.common.MessageType;
import com.chat.common.User;
import com.chat.utils.Md5;

import javax.swing.*;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/*
  该类完成用户登录验证和用户注册等功能
 */
public class UserClientService {
      private static final User user=new User();
      private static final Message msg=new Message();
      private static Socket socket;

      //通过验证username和pwd导服务器验证用户是否合法
      public static int checkUser(String username, String pwd){
               int b = 0;
            String Password;
            Local.pwd=pwd;
          Md5 k=new Md5();
          Password=k.digest(pwd);
          user.setUsername(username);
          user.setPassword(Password);
          msg.setData(user);
          try {
              socket=new Socket(InetAddress.getByName("127.0.0.1"),9999);
              ObjectOutputStream oos=new ObjectOutputStream(socket.getOutputStream());
              msg.setMesType(MessageType.MESSAGE_LOGIN);
              oos.writeObject(msg);

              ObjectInputStream ois=new ObjectInputStream(socket.getInputStream());
              Message ms=(Message) ois.readObject();

              if(ms.getMesType().equals(MessageType.MESSAGE_LOGIN_SUCCEED)){
                  Local.user=user;
                  //创建线程池
                  ExecutorService service= Executors.newFixedThreadPool(20);

                  ClientConnectServerThread clientConnectServerThread=null;

                  service.execute(clientConnectServerThread=new ClientConnectServerThread(socket));

                  ManageClientConnectServerThread.addClientConnectServerThread(user.getUsername(), clientConnectServerThread);

                  GetUserMessage.getusermessage();//将当前用户的所有信息加载到本地缓存当中
                  b=1;
              }
              else if(ms.getMesType().equals(MessageType.MESSAGE_LOGIN_REPEAT)){
                    b=2;
              }
              else{
                    socket.close();
              }
          } catch (IOException | ClassNotFoundException e) {
              e.printStackTrace();
          }
          return b;
      }
      //邮箱验证成功之后，通过邮箱获取到用户的用户名
       public static int getUser(String email){
           int b=0;
           try {
               socket=new Socket(InetAddress.getByName("127.0.0.1"),9999);
               ObjectOutputStream oos=new ObjectOutputStream(socket.getOutputStream());
               msg.setMesType(MessageType.MESSAGE_GET_USER_MSG_EMAIL);
               User user=new User();
               user.setMailbox(email);
               msg.setData(user);
               oos.writeObject(msg);

               ObjectInputStream ois=new ObjectInputStream(socket.getInputStream());
               Message ms=(Message) ois.readObject();

               if(ms.getMesType().equals(MessageType.MESSAGE_GET_USER_MSG_EMAIL)){
                   User user1= (User) ms.getData();
                   Local.user=user1;
                   //创建线程池
                   ExecutorService service= Executors.newFixedThreadPool(20);

                   ClientConnectServerThread clientConnectServerThread=null;

                   service.execute(clientConnectServerThread=new ClientConnectServerThread(socket));

                   ManageClientConnectServerThread.addClientConnectServerThread(user1.getUsername(), clientConnectServerThread);

                   GetUserMessage.getusermessage();//将当前用户的所有信息加载到本地缓存当中
                   b=1;
               }

           } catch (IOException | ClassNotFoundException e) {
               e.printStackTrace();
           }
           return b;
       }

       public static void registerUser(User user){
           try {
               socket=new Socket(InetAddress.getByName("127.0.0.1"),9999);
               ObjectOutputStream oos=new ObjectOutputStream(socket.getOutputStream());
               msg.setMesType(MessageType.MESSAGE_REGISTER);
               msg.setData(user);
               oos.writeObject(msg);
               oos.flush();

           } catch (IOException e) {
               e.printStackTrace();
           }finally {
               try {
                   socket.close();
               } catch (IOException e) {
                   e.printStackTrace();
               }
           }
       }

}
